package com.example.graduate.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.graduate.conf.Exception.AccountException;
import com.example.graduate.dto.*;
import com.example.graduate.env.RedisKeyConstants;
import com.example.graduate.env.ResponseCodeEnum;
import com.example.graduate.mapper.UserMapper;
import com.example.graduate.response.DataResponse;
import com.example.graduate.service.UserService;
import com.example.graduate.token.JudgeToken;
import com.example.graduate.util.MobileUtils;
import com.example.graduate.util.RedisUtil;
import com.example.graduate.util.SmsUtils;
import com.example.graduate.util.UuidUtils;
import com.mysql.cj.util.StringUtils;
import com.sun.deploy.security.ValidationState;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.Min;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
@RequestMapping(value = "/account", produces = MediaType.APPLICATION_JSON_VALUE)
public class AcountController {

    @Autowired
    SmsUtils smsUtils;

    @Autowired
    UuidUtils uuidUtils;

    @Autowired
    RedisUtil redisUtil;

    @Autowired
    UserService userService;


    /**
     * 获取注册验证码
     * @param request
     * @return
     */
    @PostMapping("/code")
    public DataResponse getCode(@RequestBody GetCodeRequest request){
        log.info("【获取验证码】请求手机号:{}", JSONObject.toJSONString(request));
        boolean sign = false;
         if (!MobileUtils.isMobile(request.getPhone())){
             log.info("【获取验证码】手机号码填写有误");
             return new DataResponse(ResponseCodeEnum.ERROR_PHONE.getCode());
         }
         SmsCode code = new SmsCode(uuidUtils.getCode());
        try {
            log.info("【获取验证码】发送短信验证码:{}",code.getCode());
            if (request.getType().equals("2")){
                sign = smsUtils.sendMessage(request.getPhone(), SmsUtils.forgetPasswordCode, JSONObject.toJSONString(code));
            }else{
                sign = smsUtils.sendMessage(request.getPhone(), SmsUtils.RegisterAccountCode, JSONObject.toJSONString(code));
            }
        } catch (Exception e) {
            log.error("【获取验证码】获取短信验证异常");
            throw new AccountException(ResponseCodeEnum.CHECK_SYSTEM.getCode());
        }
        if (sign){
            redisUtil.setEx(RedisKeyConstants.getVerifyCode(request.getPhone(),request.getType()),code.getCode(),2L, TimeUnit.MINUTES);
        }
        return new DataResponse(ResponseCodeEnum.success.getCode());
    }

    /**
     * 验证码校验
     * @param request
     * @return
     */
    @PostMapping("/code/verify")
    public DataResponse verify(@RequestBody VerifyRequest request){
        log.info("【验证码验证】请求:{}",JSONObject.toJSONString(request));
        String code = redisUtil.get(RedisKeyConstants.getVerifyCode(request.getPhone(), request.getType()));
        if (Objects.isNull(code)|| !code.equals(request.getCode())){
            log.info("【验证码验证】验证错误，发送验证码为:{},正确为:{}",request.getCode(),code);
            return new DataResponse(ResponseCodeEnum.VERIFY_CODE_ERROR.getCode());
        }
        redisUtil.delete(RedisKeyConstants.getVerifyCode(request.getPhone(), request.getType()));
        return new DataResponse(ResponseCodeEnum.success.getCode());
    }


    /**
     * 账号注销
     * @param freezeAcountRequest
     * @return
     */
    @PostMapping("/freeze")
    @JudgeToken
    public DataResponse freeze(@RequestBody FreezeAcountRequest freezeAcountRequest){
        log.info("【账号注销接口】注销信息：{}", JSONObject.toJSONString(freezeAcountRequest));
        DataResponse dataResponse = userService.freezeAccount(freezeAcountRequest);
        log.info("【账号注销接口】响应信息：{}",JSONObject.toJSONString(dataResponse));
        return dataResponse;
    }

    /**
     * 账号注销
     * @param registerAcountRequest
     * @return
     */
    @PostMapping("/register")
    public DataResponse register(@RequestBody RegisterAcountRequest registerAcountRequest){
        log.info("【账号注册接口】注册信息：{}", JSONObject.toJSONString(registerAcountRequest));
        DataResponse dataResponse = userService.registerAccount(registerAcountRequest);
        log.info("【账号注册接口】响应信息：{}",JSONObject.toJSONString(dataResponse));
        return dataResponse;
    }
    /**
     * 密码重置
     * @param resetPasswordRequest
     * @return
     */
    @PostMapping("/resetPassword")
    @JudgeToken
    public DataResponse resetPassword(@RequestBody ResetPasswordRequest resetPasswordRequest){
        log.info("【密码重置接口】请求信息：{}", JSONObject.toJSONString(resetPasswordRequest));
        DataResponse dataResponse = userService.resetPassword(resetPasswordRequest);
        log.info("【密码重置接口】响应信息：{}",JSONObject.toJSONString(dataResponse));
        return dataResponse;
    }

    @PostMapping("/chanceAcountStatus")
    @JudgeToken
    public DataResponse chanceAcountStatus(@RequestBody ChanceAccountStatusRequest chanceAccountStatusRequest){
        log.info("【更改账户状态】请求信息：{}", JSONObject.toJSONString(chanceAccountStatusRequest));
        DataResponse dataResponse = userService.chanceAccountStatus(chanceAccountStatusRequest);
        log.info("【更改账户状态】响应信息：{}",JSONObject.toJSONString(dataResponse));
        return dataResponse;
    }

}
